import { getItem } from '@/utils/storage';

import { Rule } from '#/entity';
import { StorageEnum } from '#/enum';

export function hasPermission(ruleStr: string) {
  const rule = stringToRule(ruleStr);
  const rules = getItem<Rule[]>(StorageEnum.Rules) || [];
  const hasRule = rules.find((item) => {
    return (
      item.operation.entity.module.module === rule.operation.entity.module.module &&
      item.operation.entity.entity === rule.operation.entity.entity &&
      item.operation.code === rule.operation.code
    );
  });
  return hasRule !== undefined;
}

export function stringToRule(ruleStr: string) {
  const strList = ruleStr.split(':');
  return {
    operation: {
      code: strList[2],
      entity: {
        entity: strList[1],
        module: { module: strList[0] },
      },
    },
  };
}
